package pers.xukai.common.constant.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import pers.xukai.common.constant.GlobalConstant;

import java.io.Serializable;

/**
 * 通用相应类
 *
 * @author 殷绪凯
 * @datetime 2022年 11月 29日 15:48
 */
@Data
@ToString
@AllArgsConstructor
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = -2214038391512787069L;
    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应码
     */
    private String code;

    /**
     * 响应内容
     */
    private T content;

    private ResponseResult(ResponseEnum responseEnum, T o) {
        this.msg = responseEnum.getMsg();
        this.code = responseEnum.getCode();
        this.content = o;
    }

    /**
     * 无返回值
     *
     * @param msg
     * @param code
     * @return
     */
    public static ResponseResult<Object> voidResponse(String msg, String code) {
        return new ResponseResult<>(msg, code, null);
    }

    /**
     * 成功响应
     *
     * @return
     */
    public static ResponseResult<Object> success() {
        return new ResponseResult<>(GlobalConstant.SUCCESS, GlobalConstant.SUCCESS_CODE, null);
    }

    /**
     * 成功响应
     *
     * @param msg  响应信息
     * @param code 响应状态码
     * @return
     */
    public static ResponseResult<Object> success(String msg, String code) {
        return new ResponseResult<>(msg, code, null);
    }

    /**
     * 错误响应
     *
     * @return
     */
    public static ResponseResult<Object> error() {
        return new ResponseResult<>(GlobalConstant.FAILURE, GlobalConstant.FAILURE_CODE, null);
    }

    /**
     * 错误响应
     * @param msg 响应信息
     * @param code 响应状态码
     * @return
     */
    public static ResponseResult<Object> error(String msg, String code) {
        return new ResponseResult<>(msg, code, null);
    }

}
